package com.dreamoo.openai.config;

import com.dreamoo.aicommon.util.SystemUtil;
import dev.langchain4j.model.image.ImageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiImageModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

import static dev.langchain4j.model.chat.Capability.RESPONSE_FORMAT_JSON_SCHEMA;

/**
 * @author 山顶洞人郑某
 * @className OpenAiConfig.java
 * @date 2025 04 29
 */
@Configuration
public class OpenAiConfig {


    private static final String OPENAI_KEY = SystemUtil.getOpenAiKey();
    private static final String MODEL_NAME = "gpt-4o-mini";
    public static final String BASE_URL = "https://api.gptgod.online/v1/";

    private static final String IMAGE_MODEL_NAME = "gpt-4o-all";

    @Bean
    public OpenAiStreamingChatModel openAiStreamingChatModel() {
        return OpenAiStreamingChatModel.builder()
                .modelName(MODEL_NAME)
                .baseUrl(BASE_URL)
                .apiKey(OPENAI_KEY)
                .timeout(Duration.ofSeconds(60))
                .build();
    }

    @Bean
    public OpenAiChatModel openAiChatModel() {

        return OpenAiChatModel.builder()
                .modelName(MODEL_NAME)
                .apiKey(OPENAI_KEY)
                .baseUrl(BASE_URL)
                .timeout(Duration.ofSeconds(60))
                .supportedCapabilities(RESPONSE_FORMAT_JSON_SCHEMA) //JSON模式
                .strictJsonSchema(true)
                .build();
    }

    @Bean("openAiImageModel")
    public ImageModel imageModel() {
        return OpenAiImageModel.builder()
                .apiKey(OPENAI_KEY)
                .modelName(IMAGE_MODEL_NAME)
                .baseUrl(BASE_URL)
                .build();
    }
}
